package org.anq.ddm.controller;

import com.amazonaws.services.s3.model.ObjectMetadata;
import org.anq.ddm.pojo.FileReturn;
import org.anq.ddm.utils.AwzS3Util;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

@RestController
@RequestMapping("/common")
public class CommonController {

    /**
     * 上传附件
     * @param file
     * @return
     */
    @PostMapping(value = "/upload")
    public FileReturn uploadObject(MultipartFile file) {
        FileReturn f = new FileReturn();
        f.setName(file.getOriginalFilename());
        System.out.println(file.getContentType());
        System.out.println(file.getOriginalFilename());

        try {
            f.setUrl("http://localhost:8080/ddm/download/"+ AwzS3Util.upload(file, null));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return f;
    }

    /**
     * 下载附件
     * @param key
     * @return
     */
    @GetMapping(value = "/download/{key}")
    public ResponseEntity downloadObject(@PathVariable String key) {
        ResponseEntity<byte[]> download = null;
        try {
            download = AwzS3Util.downloadByName(null, key);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return download;
    }

    /**
     * 查看附件信息
     * @param key
     * @return
     */
    @PostMapping(value = "/getFileInfo")
    public ObjectMetadata getFileInfo(String key) {
        return AwzS3Util.getFileInfo(null, key);
    }

}
